﻿<#include "/common/easyui-css.html">
<#include "/common/easyui-js.html">
<#include "/common/loading.html">
<script type="text/javascript" src="/lib/layer/2.4/layer.js"></script>

<body class="easyui-layout">

<div data-options="region:'center',border:false" style="padding:2px;height: auto">
	<table id="role_list_table" class="easyui-datagrid" title="" style="width:100%;height:100%;padding-bottom: 2px;"
		   data-options="
		   	 rownumbers:true,
		   	 pagination:true,
		   	 singleSelect:true,
		   	 toolbar : '#tb',
		   	 fitColumns : true,
		   	 url:'/role-list-json/1/10',
           method:'get',
           onAfterEdit:function(index,row){
           	var rolename = row.roleName;
           	var rights = row.rights;
           	var id = row.id;
           	var btnRights = row.btnRights;
				updateActions(rolename,rights,id,btnRights);
			 },
           showFooter: true"
		>
		<thead>
		<tr>
			<th data-options="field:'roleName',width:90,align:'center',editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'请填写角色名称'
					}
					}">角色名称</th>
			<th data-options="field:'rights',width:90,align:'center'" >权限码</th>
			<th data-options="field:'btnRights',width:90,align:'center'" >权限码</th>
        </tr>
		</thead>
	</table>
</div>


<div id="tb" style="padding:5px;height:auto">
	<#include "/common/button.html">
</div>

<script type="text/javascript">

	//分配权限
	$("#assign").click(function(){
		var row = $('#role_list_table').datagrid('getSelected');
		if(!row){
			$.messager.show({
				title:'提示',
				msg:'请选择需要分配权限的角色',
				timeout:3000,
				showType:'slide',
			});
		}else{
			layer.open({
				type: 2,
				title: false,
				closeBtn: 0,
				shadeClose: true,
				area: ['757px', '300px'],
				skin: 'demo-class',
				content: '/admin-role-assign/'+row.rights+'/'+row.id
			});
		}
	})

	$('#assignButton').click(function(){
        var row = $('#role_list_table').datagrid('getSelected');
        if(!row){
            $.messager.show({
                title:'提示',
                msg:'请选择需要分配权限的角色',
                timeout:3000,
                showType:'slide',
            });
        }else{
            layer.open({
                type: 2,
                title: false,
                closeBtn: 0,
                shadeClose: true,
                area: ['757px', '300px'],
                skin: 'demo-class',
                content: '/admin-button-assign/'+row.rights+'/'+row.btnRights+'/'+row.id
            });
        }
	});

	//编辑
	$("#edit").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if (row){
			var index = $('#role_list_table').datagrid('getRowIndex', row);
            $('#role_list_table').datagrid('selectRow',index);
            $('#role_list_table').datagrid('beginEdit',index);
		} else {
            $.messager.show({
                title:'提示',
                msg:'请选择需要编辑的角色',
                timeout:3000,
                showType:'slide',
            });
		}

	})

	//保存
	$("#save").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if (row){
			var index = $('#role_list_table').datagrid('getRowIndex', row);
		} else {
			index = 0;
		}
		$('#role_list_table').datagrid('endEdit',index);
	})

	//新增
	$("#add").click(function () {
        var url = '/admin-role-add';
        var index = layer.open({
            title:'用户添加',
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: false,
            content: url
        });
	})

	//删除
	$("#delete").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if(!row){
			$.messager.show({
				title:'提示',
				msg:'请选择需要删除的角色',
				timeout:3000,
				showType:'slide',
			});
		}else{
			$.messager.confirm('删除信息','确定删除用户:'+row.roleName+"吗?",function(r){
				if (r){
					$.ajax({
						url : '/role-ins-upd-del/d',
						data : {role_id : row.id},
						async : true,
						success : function(){
							$('#role_list_table').datagrid('reload');
						}
					});
				}
			});
		}
	})
</script>
<script>
	function updateActions(rolename,rights,id,btnRights){
		var url = '/role-ins-upd-del/';
		//修改
		if(id!=null&&id!=''){
			url += 'u';
		}else{
		    url += 'i'
		}
		$.ajax({
			url : url,
			data : {role_name : rolename,role_id:id,rights:rights,btnRights:btnRights,tm:new Date()},
			async : true,
			type : 'post',
			success :function () {
				$('#role_list_table').datagrid('reload');
			},
			error : function () {
				alert('error');
			}
		})
	}
</script>
